#include <bits/stdc++.h>
using std::cin;
using std::cout;
typedef long long ll;
typedef std::pair <int, int> pr;
typedef std::tuple <int, int, int> tuple;
const int N = 50054, M = N * 2, INF = 0x3f3f3f3f;
int n, m, E = 0, L, QC = 0, QD = 0, EV;
int to[M], first[N], next[M];
bool is_leaf[N];
int p[N], deg[N], leaves[N], ans[N];
int cnt = 0, id[N], eid[N];
int count[N];

pr e[200054], ee[200054], queD[300054], queC[300054];
tuple event[1200054];
int ret[300054];

inline void down(int &x, const int y) {x > y ? x = y : 0;}

inline void addedge(int u, int v) {
	to[++E] = v, next[E] = first[u], first[u] = E;
	to[++E] = u, next[E] = first[v], first[v] = E;
}

void dfs(int x) {
	int i, y; id[x] = ++cnt, count[x] = deg[x];
	for (i = first[x]; i; i = next[i])
		if ((y = to[i]) != p[x]) p[y] = x, dfs(y), count[x] += count[y];
	eid[x] = cnt;
}

void dfs2(int x) {
	int i, y;
	for (i = first[x]; i; i = next[i])
		if (p[y = to[i]] == x) ret[y] = ret[x], dfs2(y);
}

inline void check(int x, int y) {
	assert(1 <= x && x <= n && 1 <= y && y <= n);
	if (x == 1 || y == 1 || x == y) return;
	if (id[x] > id[y]) std::swap(x, y);
	if (id[y] <= eid[x]) queC[QC++] = pr(x, y);
	else queD[QD++] = pr(x, y);
}

namespace BIT {
	int x[N];

	int sum(int h) {int s = 0; for (; h > 0; h &= h - 1) s += x[h]; return s;}
	void add(int h) {for (; h <= n; h |= h - 1, ++h) ++x[h];}
}

void work() {
	int i, j, k, x, y;
	for (i = 0; i < QC; ++i)
		std::tie(x, y) = queC[i], ret[i] = count[x] - count[y],
		down(ans[x], ret[i]), down(ans[y], ret[i]);

	for (i = 0; i < m; ++i) e[i] = std::minmax(id[e[i].first], id[e[i].second]);
	std::sort(e, e + m);
	for (EV = i = 0; i < QD; ++i)
		std::tie(x, y) = queD[i], ret[i] = count[x] + count[y],
		event[EV++] = tuple(id[x] - 1, id[y] - 1, ~i),
		event[EV++] = tuple(id[x] - 1, eid[y], i),
		event[EV++] = tuple(eid[x], id[y] - 1, i),
		event[EV++] = tuple(eid[x], eid[y], ~i);
	std::sort(event, event + EV);
	for (j = k = 0, y = 1; y <= n; ++y) {
		for (; j < m && e[j].first == y; ++j) BIT::add(e[j].second);
		for (; k < EV && std::get<0>(event[k]) == y; ++k)
			std::tie(y, x, i) = event[k], i >> 31 ? ret[~i] -= 2 * BIT::sum(x) : ret[i] += 2 * BIT::sum(x);
	}
	for (i = 0; i < QD; ++i) std::tie(x, y) = queD[i], down(ans[x], ret[i]), down(ans[y], ret[i]);
}

int main() {
    freopen("cut.in","r",stdin);
    freopen("cut.out","w",stdout);
	int i, j, u, v, w;
	std::ios::sync_with_stdio(false), cin.tie(NULL);
	cin >> n >> m, m -= n - 1;
	for (i = 1; i < n; ++i) cin >> u >> v, addedge(u, v);
	for (i = 0; i < m; ++i) cin >> u >> v, e[i] = pr(u, v), ++deg[u], ++deg[v];
	dfs(1), memset(is_leaf, true, n + 1);
	for (i = 1; i <= n; ++i) is_leaf[p[i]] = false, ans[i] = INF;

	ret[1] = 1;
	for (L = 0, i = first[1]; i; i = next[i]) leaves[L++] = to[i], dfs2(ret[to[i]] = to[i]);
	for (i = 0; i < m; ++i) ee[i] = std::minmax(ret[e[i].first], ret[e[i].second]);
	std::sort(ee, ee + m);
	for (j = 0, i = 1; i <= m; ++i)
		if (i == m || ee[i] != ee[i - 1]) {
			std::tie(u, v) = ee[j];
			if (u == 1 || v == 1) {j = i; continue;}
			assert(u != v);
			w = count[u] + count[v] - 2 * (i - j);
			down(ans[u], w), down(ans[v], w), j = i;
		}

	std::sort(leaves, leaves + L, [] (const int x, const int y) {return count[x] < count[y];});
	for (i = 1; i < L; ++i)
		u = *leaves, v = leaves[i], down(ans[u], count[u] + count[v]), down(ans[v], count[u] + count[v]);
	
	for (L = 0, i = 1; i <= n; ++i) if (is_leaf[i]) leaves[L++] = i;
	std::sort(leaves, leaves + L, [] (const int x, const int y) {return deg[x] < deg[y];});
	for (i = 2; i <= n; ++i) check(i, p[i]);
	for (i = 0; i < m; ++i) check(e[i].first, e[i].second);
	assert(L > 0);
	for (i = 1; i <= n; ++i) check(i, *leaves);
	if (L > 1) for (i = 1; i <= n; ++i) check(i, leaves[1]);
	work();

	for (i = 1; i < E; i += 2)
		u = to[i + 1], v = to[i],
		cout << 2 + ans[ p[u] == v ? u : v ] << (i == E - 1 ? '\n' : ' ');
	return 0;
}
